package com.spike.servlet;

import com.spike.model.User;
import com.spike.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String ifCheck = req.getParameter("ifCheck");
        UserService userService = new UserService();
        User user = userService.login(username, password);
        if (user != null) {
            req.setAttribute("loginUser", user);
            req.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(req, resp);
        }else {
            if (ifCheck != null && ifCheck.equals("on")) {
                req.setAttribute("username",username);
                req.setAttribute("password",password);
            }
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }

    }
}
